package com.inossem.utils.signaturePad;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.github.gcacace.signaturepad.views.SignaturePad;
import com.inossem.utils.R;
import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;

import java.io.File;
import java.util.UUID;

public class SignaturePadActivity extends Activity {

    private static final String SUFFIX_JPG = ".jpg";

    private static final String SUFFIX_PNG = ".png";

    private SignaturePad signaturePad;

    private Button clearSignature, makingSignature, exit;

    private TextView areaPrompt;

    private int type;

    private String imagePath;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView(SignaturePadActivity.this);
        initData(SignaturePadActivity.this);
    }

    private void initView(Activity activity) {
        setContentView(R.layout.utils_activity_signature_pad);
        areaPrompt = (TextView) activity.findViewById(R.id.areaPrompt);
        clearSignature = (Button) activity.findViewById(R.id.clearSignature);
        makingSignature = (Button) activity.findViewById(R.id.makingSignature);
        exit = (Button) activity.findViewById(R.id.exit);
        signaturePad = (SignaturePad) activity.findViewById(R.id.signaturePad);
    }

    private void initData(final Activity activity) {
        Intent intent = activity.getIntent();
        type = intent.getIntExtra(SignaturePadUtils.TYPE, SignaturePadUtils.TYPE_NORMAL);
        imagePath = intent.getStringExtra(SignaturePadUtils.IMAGE_PATH);
        areaPrompt.setText(intent.getStringExtra(SignaturePadUtils.AREA_PROMPT));
        exit.setText(intent.getStringExtra(SignaturePadUtils.EXIT));
        clearSignature.setText(intent.getStringExtra(SignaturePadUtils.CLEAR_SIGNATURE));
        makingSignature.setText(intent.getStringExtra(SignaturePadUtils.MAKING_SIGNATURE));

        clearSignature.setEnabled(false);
        makingSignature.setEnabled(false);

        clearSignature.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                if (!signaturePad.isEmpty()) {
                    signaturePad.clear();
                }
            }
        });

        makingSignature.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                if (!signaturePad.isEmpty()) {

                    File signatureFile = null;
                    String name = UUID.randomUUID().toString().replace("\\-", "");
                    switch (type) {
                        case SignaturePadUtils.TYPE_NORMAL:
                            signatureFile = Utils.bitmapToFile(imagePath + File.separator + name + SUFFIX_JPG, signaturePad.getSignatureBitmap());
                            break;
                        case SignaturePadUtils.TYPE_TRANSPARENT:
                            signatureFile = Utils.bitmapToFile(imagePath + File.separator + name + SUFFIX_PNG, signaturePad.getTransparentSignatureBitmap());
                            break;
                        case SignaturePadUtils.TYPE_TRANSPARENT_STRETCHING:
                            signatureFile = Utils.bitmapToFile(imagePath + File.separator + name + SUFFIX_PNG, signaturePad.getTransparentSignatureBitmap(true));
                            break;
                    }
//                    Utils.middleToast(activity, activity.getResources().getString(R.string.utils_signature_pad_image_save_path) + signatureFile.getAbsolutePath());
                    Intent resultIntent = new Intent();
                    resultIntent.putExtra(SignaturePadUtils.SIGNATURE_FILE_PATH, signatureFile.getAbsolutePath());
                    resultIntent.putExtra(SignaturePadUtils.IMAGE_NAME, name);
                    setResult(Activity.RESULT_OK, resultIntent);
                    finish();
                } else {
                    Utils.middleToast(activity, activity.getResources().getString(R.string.utils_signature_pad_please_signature));
                }
            }
        });

        exit.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });

        signaturePad.setOnSignedListener(new SignaturePad.OnSignedListener() {
            @Override
            public void onStartSigning() {
            }

            @Override
            public void onSigned() {
                clearSignature.setEnabled(true);
                makingSignature.setEnabled(true);
            }

            @Override
            public void onClear() {
                clearSignature.setEnabled(false);
                makingSignature.setEnabled(false);
            }
        });
    }
}
